using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using SharePointing.Features.MetaTags.Controls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Publishing;
using System.IO;
using System.Xml;
using System.Text;

namespace SharePointing.Features.MetaTags.WebParts
{
    /// <summary>
    /// This web part is for editing the page level metatags (and viewing the page level tags)
    /// </summary>
    [Guid("37441CE4-FB0C-4970-9942-82099CE3A7D6")]
    public class MetaTagPageEditor : System.Web.UI.WebControls.WebParts.WebPart
    {
        public MetaTagPageEditor()
        {
        }

        protected TextBox kwTextBox = new TextBox();
        protected TextBox authorTextBox = new TextBox();
        protected TextBox descTextBox = new TextBox();
        protected CheckBox cbOverride = new CheckBox();

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Table table = new Table();
            #region Merge Selection
            TableRow mergeRow = new TableRow();
            TableCell mergeLabelCell = new TableCell();
            TableCell mergeDataCell = new TableCell();
            Label mergeLabel = new Label();
            mergeLabel.Text = "Override Global Keywords ?";
            mergeLabelCell.Controls.Add(mergeLabel);
            mergeDataCell.Controls.Add(cbOverride);
            mergeRow.Cells.Add(mergeLabelCell);
            mergeRow.Cells.Add(mergeDataCell);
            table.Rows.Add(mergeRow);
            #endregion

            #region Keywords
            TableRow kwRow = new TableRow();
            TableCell kwLabelCell = new TableCell();
            TableCell kwDataCell = new TableCell();
            Label kwLabel = new Label();
            kwLabel.Text = "Keyword(s)";
            kwLabelCell.Controls.Add(kwLabel);
            kwDataCell.Controls.Add(kwTextBox);
            kwRow.Cells.Add(kwLabelCell);
            kwRow.Cells.Add(kwDataCell);
            table.Rows.Add(kwRow);
            #endregion

            #region Author
            TableRow aRow = new TableRow();
            TableCell aLabelCell = new TableCell();
            TableCell aDataCell = new TableCell();
            Label authorLabel = new Label();
            authorLabel.Text = "Author";
            aLabelCell.Controls.Add(authorLabel);
            aDataCell.Controls.Add(authorTextBox);
            aRow.Cells.Add(aLabelCell);
            aRow.Cells.Add(aDataCell);
            table.Rows.Add(aRow);
            #endregion

            #region Description
            TableRow dRow = new TableRow();
            TableCell dLabelCell = new TableCell();
            TableCell dDataCell = new TableCell();
            Label descLabel = new Label();
            descLabel.Text = "Description";
            dLabelCell.Controls.Add(descLabel);
            dDataCell.Controls.Add(descTextBox);
            dRow.Cells.Add(dLabelCell);
            dRow.Cells.Add(dDataCell);
            table.Rows.Add(dRow);
            #endregion

            #region Save and Clear Buttons
            TableRow bRow = new TableRow();
            TableCell bClearCell = new TableCell();
            TableCell bSaveCell = new TableCell();
            Button clearButton = new Button();
            clearButton.Text = "Clear";
            clearButton.Click += new EventHandler(clearButton_Click);
            Button saveButton = new Button();
            saveButton.Text = "Save";
            saveButton.Click += new EventHandler(saveButton_Click);
            bSaveCell.Controls.Add(clearButton);
            bSaveCell.Controls.Add(saveButton);
            bRow.Cells.Add(bClearCell);
            bRow.Cells.Add(bSaveCell);
            table.Rows.Add(bRow);
            #endregion

            this.Controls.Add(table);

            // Set the values of the text boxes based upon
            // what is stored in the PageTag
            if(!this.Page.IsPostBack)
            {
                string xmlString = SPContext.Current.ListItem.GetFormattedValue("MetaTagPageTags");
                if(String.IsNullOrEmpty(xmlString) == false)
                {
                    // There is a value stored in the field, let's deserialize it
                    PageTag pageTag = Common.Utilities.DeserializePageTag(xmlString);
                    cbOverride.Checked = pageTag.Override;
                    kwTextBox.Text = pageTag.Keywords;
                    authorTextBox.Text = pageTag.Author;
                    descTextBox.Text = pageTag.Description;
                }                
            }
        }

        void saveButton_Click(object sender, EventArgs e)
        {
            PageTag pageTag = new PageTag();
            pageTag.Override = cbOverride.Checked;
            pageTag.Author = authorTextBox.Text;
            pageTag.Description = descTextBox.Text;
            pageTag.Keywords = kwTextBox.Text;

            // Save the Page Tag to the item
            SPContext.Current.Web.AllowUnsafeUpdates = true;
            SPListItem currItem = SPContext.Current.ListItem;
            currItem["MetaTagPageTags"] = Common.Utilities.SerializePageTag(pageTag);
            currItem.Update();
            SPContext.Current.Web.AllowUnsafeUpdates = false;
        }

        void clearButton_Click(object sender, EventArgs e)
        {
            // Clears out the MetaTags stored for this page
            SPContext.Current.Web.AllowUnsafeUpdates = true;
            SPListItem currItem = SPContext.Current.ListItem;
            currItem["MetaTagPageTags"] = String.Empty;
            currItem.Update();
            SPContext.Current.Web.AllowUnsafeUpdates = false;

            kwTextBox.Text = string.Empty;
            authorTextBox.Text = string.Empty;
            descTextBox.Text = string.Empty;
            cbOverride.Checked = false;
        }

        // Retrieves the PageTags from the current page
        private string GetCurrentPageTags()
        {
            return Common.Utilities.CleanCharacterSequences(SPContext.Current.ListItem.GetFormattedValue("MetaTagPageTags"));
        }

        // Gets a reference to the current PublishingPage we are on
        private PublishingPage GetCurrentPage()
        {
            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(SPContext.Current.Web);
            PublishingPage currentPage = PublishingPage.GetPublishingPage(SPContext.Current.ListItem);
            return currentPage;
            

        }

        private LiteralControl GetSpacer()
        {
            LiteralControl spacer = new LiteralControl("<BR><BR>");
            return spacer;
        }
    }
}
